package com.example.netcomponent.net.upload;

import android.content.Context;


import com.example.netcomponent.net.HttpOnNextListener;
import com.example.netcomponent.net.utils.LogUtils;

import rx.Subscriber;

/**
 * Created by zengxianghui900 on 17/10/4.
 * 自定义带加载框的subscriber,请求开始展示
 * 完成则结束，加载框内容可自定义
 */

public class UploadPicSubscriber<T> extends Subscriber<T> {
    /**请求数据*/
    private BaseUploadApi mBaseApi;
    /**接口*/
    private HttpOnNextListener mHttpOnNextListener;

    /**上下文*/
    private Context mActivity;

    public UploadPicSubscriber(BaseUploadApi<T> baseApi) {
        this.mBaseApi = baseApi;
        this.mHttpOnNextListener = baseApi.getListener();
        this.mActivity = baseApi.getContext();
    }



    @Override
    public void onStart() {

    }



    @Override
    public void onCompleted() {

    }



    @Override
    public void onError(Throwable e) {
        //对错误进行处理
        doError(e);
    }



    @Override
    public void onNext(T t) {
        HttpOnNextListener listener = mHttpOnNextListener;
        if (t != null && listener != null) {
            listener.onNext(t);
        }
    }

    /**
     * 统一处理错误信息
     * @param e
     */
    private void doError(Throwable e) {
//        Context context = mActivity;
//        if (context == null) return;
//        if (e instanceof SocketTimeoutException) {
//            ToastUtils.showShortToast(context, AppConstant.Net.SOCKET_TIMEOUT_FAIL);
//        } else if (e instanceof ConnectException) {
//            ToastUtils.showShortToast(context, AppConstant.Net.CONNECT_FAIL);
//        }else if (e instanceof java.net.UnknownHostException) {     //无法解决地址名
//            ToastUtils.showShortToast(context, AppConstant.Net.OTHER_CONNECT_FAIL);
//        }  else {
//            ToastUtils.showShortToast(context, e.getMessage());
//            e.printStackTrace();
//        }
        //将日志存储一下
        LogUtils.saveLogToSD(LogUtils.UPLOAD_PIC,"UploadPic====Message:" + e.getMessage());

        if (mHttpOnNextListener != null) {
            mHttpOnNextListener.onError(e);
        }
    }
}
